/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package app.model_package;

import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author haythem
 */
public class CaseModel {
    private String caseID;
    private ArrayList<EventModel> eventsList ;

    public CaseModel(String caseID, ArrayList<EventModel> eventsList) {
        this.caseID = caseID;
        this.eventsList = eventsList;
    }

    public CaseModel(String caseID) {
        this.caseID = caseID;
        this.eventsList = new ArrayList<EventModel>();
    }

    public String getCaseID() {
        return caseID;
    }

    public ArrayList<EventModel> getEventsList() {
        return eventsList;
    }

    public void setCaseID(String caseID) {
        this.caseID = caseID;
    }

    public void setEventsList(ArrayList<EventModel> eventsList) {
        this.eventsList = eventsList;
    }

    public void addEvent(EventModel event) {
        this.eventsList.add(event);
    }

    public void deleteEvent(EventModel event) {
        this.eventsList.remove(event);
    }

    public String toString(){
        String s= "<case name='"+this.caseID+"'> \n" ;

        for(int i=0;i<eventsList.size();i++){
            EventModel e = eventsList.get(i);
             s = s+ e.toString();
        }
        s+="</case> \n";
        return s ;
    }


    /**
     * this method deletes the event having
     * the ID eventID
     * @param eventID
     */
    public void deleteEvent(String eventID){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.remove(i);
        }
        else {
            System.out.println(eventID+" doesn't exist !!");
        }
    }


    /**
     * this method returns the index of the
     * event with the ID eventID if it exists
     * in eventsList else returns -1
     * @param eventID : the case's ID we look for
     * @return : the event index in the list of cases
     */
    public int lookForEvent(String eventID){
        boolean b = false;
        int i = 0 ;
        // for cases list
        while ((i<eventsList.size()) && (b == false)){
            // we look for the case with the ID
            // previousCaseID
            b = this.eventsList.get(i).getEventID().equals((String)eventID);
            if (b==false) {i++;}
        }
        if (b==true){
            return i;
        }
        else {return -1;}
    }


    /**
     *
     * @param eventID
     * @param taskID
     */
    public void modifTask(String eventID, String previousTaskID,String newtaskID){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifTask(previousTaskID,newtaskID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }


    /**
     * 
     * @param eventID
     * @param previousIniatorID
     * @param newIniatorID
     */
    public void modifIniator(String eventID,String previousIniatorID,String newIniatorID){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifIniator(previousIniatorID,newIniatorID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }

    /**
     *
     * @param eventID
     * @param previousReceiverID
     * @param newReceiverID
     */
    public void modifReceiver(String eventID,String previousReceiverID,String newReceiverID){
         // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifReceiver(previousReceiverID,newReceiverID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }



    /**
     *
     * @param eventID
     * @param ReceiverID
     */
    public void deleteReceiver(String eventID,String ReceiverID){
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).deleteReceiver(ReceiverID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }



    /**
     *
     * @param eventID
     * @param ReceiverID
     */
    public void addReceiver(String eventID,String ReceiverID){
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).addReceiver(ReceiverID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }

    /**
     *
     * @param eventID
     * @param previousPerformativ
     * @param newPerformativ
     */
    public void modifPerformativ(String eventID,String previousPerformativ,String newPerformativ){
         // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifPerformativ(previousPerformativ,newPerformativ);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }


    /**
     * 
     * @param eventID
     * @param previousDate
     * @param newDate
     */
    public void modifDate(String eventID,Date previousDate,Date newDate){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifDate(previousDate, newDate);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }

    /**
     * 
     * @param eventID
     * @param previousConversation
     * @param newConversation
     */
    public void modifConversation(String eventID,String previousConversation,String newConversation){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).modifConversation(previousConversation, newConversation);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }


    public void modifConversationInCase(String previousConversation,String newConversation){
            for (int i=0;i<eventsList.size();i++){
            if (eventsList.get(i).getConversationID().equals((String) previousConversation)){
                eventsList.get(i).setConversationID(newConversation);
            }
        }
    }





    /**
     * 
     * @param eventID
     * @param taskID
     * @param initiator
     * @param listReceivers
     * @param perfomative
     * @param date
     */
    public void editEvent(String eventID, String taskID,String initiator, ArrayList<String> listReceivers,String perfomative, Date date){
        // the index of the event
        int i = lookForEvent(eventID);
        if ((i>=0) && (i<this.eventsList.size())){
            this.eventsList.get(i).setDate(date);
            this.eventsList.get(i).setIniator(initiator);
            this.eventsList.get(i).setListReceivers(listReceivers);
            this.eventsList.get(i).setPerfomative(perfomative);
            this.eventsList.get(i).setTask(taskID);
        }
        else {
            System.out.println(eventID+" doesn't exist (event) !!");
        }
    }


    /**
     * deletes all the events having the taskID
     * as a task
     * @param taskID
     */
    public void deleteTaskInCase(String taskID){
        for (int i=0;i<eventsList.size();i++){
            if (eventsList.get(i).getTask().equals((String) taskID)){
                eventsList.remove(i);
            }
        }
    }



    /**
     * edits task ID in all events 
     * @param previousTaskID
     * @param newTaskID
     */
   public void editTaskInCase(String previousTaskID,String newTaskID){
       for (int i=0;i<eventsList.size();i++){
            if (eventsList.get(i).getTask().equals((String) previousTaskID)){
                eventsList.get(i).setTask(newTaskID);
            }
        }
   }


   /**
    * in all events in this case this method looks if the some
    * of the agents operating have as ID previousAgentID and
    * replace it by newAgentID
    * @param previousAgentID
    * @param newAgentID
    */
   public void editAgentInCase(String previousAgentID,String newAgentID){
       for (int i=0;i<eventsList.size();i++){
            eventsList.get(i).editAgentInCase(previousAgentID,newAgentID);
        }
   }


   /**
    * edits perfomative in all events
    * @param previousPerformativ
    * @param newPerformativ
    */
   public void editPerformativInCase(String previousPerformativ,String newPerformativ){
       for (int i=0;i<eventsList.size();i++){
            if (eventsList.get(i).getPerfomative().equals((String) previousPerformativ)){
                eventsList.get(i).setPerfomative(newPerformativ);
            }
        }
   }


   /**
    * edits all the dates previousDate that are
    * in the events
    * @param previousDate
    * @param newDate
    */
   public void modifDateInCase(Date previousDate,Date newDate){
       for (int i=0;i<eventsList.size();i++){
            if (eventsList.get(i).getDate().equals((Date) previousDate)){
                eventsList.get(i).setDate(newDate);
            }
        }
   }



   /**
    * this method adds the events of the case
    * c to the actual case
    * @param c
    */
   public void addCase(CaseModel c){
       ArrayList<EventModel> firstList = new ArrayList<EventModel>();
       ArrayList<EventModel> sortedList = new ArrayList<EventModel>();
       firstList.addAll(this.eventsList);
       firstList.addAll(c.getEventsList());
       while (firstList.size()>0){
           int indexMin = getIdexMinDate(firstList);
           sortedList.add(firstList.get(indexMin));
           firstList.remove(indexMin);
       }
       this.setEventsList(sortedList);
   }

   /**
    * this method returns the event that happens first
    * in the list of events given as a parameter
    * @param listEvents
    * @return
    */
   public int getIdexMinDate(ArrayList<EventModel> listEvents){
       // we suppose that the smallest (Date) is the first in
       // the list
       int indexMin = 0;
       for (int i=1;i<listEvents.size();i++){
           Date date1 = listEvents.get(indexMin).getDate();
           Date date2 = listEvents.get(i).getDate();
           int j = date1.compareTo(date2);
           if (j>0){
               indexMin = i;
           }
       }
       return indexMin;
   }




}
